package com.travel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianfeng.util.ResultMsg;
import com.travel.entity.*;
import com.travel.mapper.GateOrderMapper;
import com.travel.mapper.UserMapper;
import com.travel.mapper.ViewMapper;
import com.travel.service.GateOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class GateOrderServiceImpl extends ServiceImpl<GateOrderMapper,TbGateOrder> implements GateOrderService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    ViewMapper viewMapper;

    @Autowired
    GateOrderMapper gateOrderMapper;

    @Override
    public ResultMsg addOrderClass(TbUser user, TbView tbView) {
        TbGateOrder gateOrder = new TbGateOrder();
        gateOrder.setGateOrderid(UUID.randomUUID().toString());
        gateOrder.setUserId(user.getUserId());
        gateOrder.setGateCreateTime(new Date());
        gateOrder.setViewName(tbView.getViewName());
        gateOrder.setGateTotal(tbView.getViewPrice());
        gateOrder.setGateStatus(0);
        boolean saveOrder = super.save(gateOrder);

        if (saveOrder==true){
            return ResultMsg.success(200);
        }
        return ResultMsg.error(1002,"生成订单错误");
    }

    @Override
    public ResultMsg addOrderId(Integer userId, Integer viewId) {
        QueryWrapper<TbView> wrapper = new QueryWrapper<>();
        wrapper.eq("view_id",viewId);
        TbView tbView = viewMapper.selectOne(wrapper);

        TbGateOrder gateOrder = new TbGateOrder();
        gateOrder.setGateOrderid(UUID.randomUUID().toString());
        gateOrder.setUserId(userId);
        gateOrder.setGateCreateTime(new Date());
        gateOrder.setViewName(tbView.getViewName());
        gateOrder.setGateTotal(tbView.getViewPrice());
        gateOrder.setGateStatus(0);
        boolean saveOrder = super.save(gateOrder);

        if (saveOrder==true){
            return ResultMsg.success(200);
        }
        return ResultMsg.error(1002,"生成订单错误");
    }

    @Override
    public ResultMsg seleteAll() {
        List<TbGateOrder> orderList = super.list();
        if (orderList!=null || orderList.size()!=0){
            return ResultMsg.success(orderList);
        }
        return ResultMsg.error(1003,"查询门票订单失败");
    }

    @Override
    public ResultMsg seleteAllByPage(Integer current, Integer size) {
        //分页
        Page<TbGateOrder> pages = new Page<>(current, size);
        Page<TbGateOrder> page = super.page(pages);
        return ResultMsg.success(page);
    }
    @Override
    public ResultMsg seleteOneById(Integer gateOrderId) {
        TbGateOrder gateOrder = super.getById(gateOrderId);
        if (gateOrder!=null) {
            return ResultMsg.success(gateOrder);
        }
        return ResultMsg.error(1003,"查询门票订单失败");
    }

    @Override
    public ResultMsg addOrder(TbGateOrder tbGateOrder, HttpServletRequest req) {
        tbGateOrder.setGateOrderid(UUID.randomUUID().toString().replace("-",""));
        tbGateOrder.setGateCreateTime(new Date());
        tbGateOrder.setGateStatus(0);
        HttpSession session = req.getSession();
        Object user = session.getAttribute("user");
        if (user!=null&&user instanceof TbUser){
            TbUser u = (TbUser) user;
            tbGateOrder.setUserId(u.getUserId());
        }
        int res = gateOrderMapper.insets(tbGateOrder);
        if (res!=0){
            return ResultMsg.success();
        }
        return ResultMsg.error(201);
    }

    @Override
    public int update(TbGateOrder order) {
        return gateOrderMapper.updateStatus(order);
    }

    @Override
    public ResultMsg success() {
        return ResultMsg.success(200);
    }
}
